package com.sunducation.wts.penbox.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.sunducation.wts.penbox.R;
import com.wetao.lib_common.bean.response.ExamDetailInfo;
import com.wetao.lib_common.bean.response.ReadPushListInfo;
import com.wetao.lib_common.bean.response.StudentExamListInfo;
import com.wetao.lib_common.bean.response.StudentLiveMessageInfo;
import com.wetao.lib_common.utils.StringUtil;

import org.jetbrains.annotations.NotNull;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * author: zhouyh
 * created on: 3/9/21 3:52 PM
 * description: 通知适配器
 */
public class NoticeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private static final int CLASS_ITEM = 0;
    private static final int EXAM_ITEM = 1;
    private static final int READPUSH_ITEM = 2;
    private int headViewCount = 1;
    private Context mContext;
    private List<StudentExamListInfo.Data.RecordsBean> examList = new ArrayList<>();

    private List<ReadPushListInfo.Data.RecordsBean> pushList = new ArrayList<>();

    private List<StudentLiveMessageInfo.Data> classDatas = new ArrayList<>();



    public NoticeAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setClassData(List<StudentLiveMessageInfo.Data> datas){
        this.classDatas.clear();
        this.classDatas.addAll(datas);
        notifyDataSetChanged();
    }
    public void setExamData(List<StudentExamListInfo.Data.RecordsBean> datas){
        this.examList.clear();
        this.examList.addAll(datas);
        notifyDataSetChanged();
    }

    public void setPushData(List<ReadPushListInfo.Data.RecordsBean> datas){
        this.pushList.clear();
        this.pushList.addAll(datas);
        notifyDataSetChanged();
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        if (viewType == CLASS_ITEM){
            View head = LayoutInflater.from(mContext).inflate(R.layout.notice_class_item,viewGroup,false);
            return new ClassNoticeViewHolder(head);
        }else if (viewType == EXAM_ITEM){
            View root = LayoutInflater.from(mContext).inflate(R.layout.notice_exam_item,viewGroup,false);
            return new ExamNoticeViewHolder(root);
        }else if(viewType == READPUSH_ITEM){
            View root = LayoutInflater.from(mContext).inflate(R.layout.notice_push_item,viewGroup,false);
            return new PushNoticeViewHolder(root);
        }
        return null;
    }



    @Override
    public void onBindViewHolder(@NonNull @NotNull RecyclerView.ViewHolder viewHolder, int pos) {
        if (viewHolder instanceof ClassNoticeViewHolder){
            ClassNoticeViewHolder temp = (ClassNoticeViewHolder) viewHolder;
            temp.tvContent.setText(classDatas.get(pos).getContent());
            temp.tvClassSubject.setText(String.format("%s互动课堂",classDatas.get(pos).getSubjectName()));
            temp.tvClassTime.setText(classDatas.get(pos).getCreateTime());
            temp.itemView.setOnClickListener(v -> {
                if (noticeItemClickListener!=null){
                    noticeItemClickListener.OnClassItemClickListener(classDatas.get(pos),pos);
                }
            });
        }else if (viewHolder instanceof ExamNoticeViewHolder){
            int index = pos - classDatas.size();
            ExamNoticeViewHolder examViewHolder = (ExamNoticeViewHolder) viewHolder;
            examViewHolder.tvTitle.setText(StringUtil.replaceAmp(examList.get(index).getExamName()));
            examViewHolder.tvSubject.setText(String.format("%s作业",examList.get(index).getSubjectName()));
            examViewHolder.tvTime.setText("答题时间:  "+examList.get(index).getAnswerTimeStart() + "-" + examList.get(index).getAnswerTimeEnd());
            examViewHolder.tvProgress.setText("答题进度:  " + examList.get(index).getAnswerAmount() + "/" + examList.get(index).getTotalQuestion());
            examViewHolder.itemView.setOnClickListener(v->{
                if (noticeItemClickListener!=null){
                    noticeItemClickListener.OnExamItemClickListener(examList.get(index),index);
                }
            });
        }else{
            int index = pos - classDatas.size() - examList.size();
            PushNoticeViewHolder holder = (PushNoticeViewHolder) viewHolder;
            holder.tvPushTitle.setText(pushList.get(index).getReadPushTitle());
            holder.tvPushTime.setText("推送时间: " + pushList.get(index).getPushTime());
            holder.tvPushSubject.setText(pushList.get(index).getSubjectName() + "阅读");
            holder.rlPushItem.setOnClickListener(v -> {
                if (noticeItemClickListener!=null){
                    noticeItemClickListener.OnPushItemClickListener(pushList.get(index));
                }
            });
        }
    }


    @Override
    public int getItemCount() {
        return classDatas.size() + examList.size() + pushList.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (isHeadView(position)){
            return CLASS_ITEM;
        }else if (position > (classDatas.size() + examList.size() - 1)){
            return READPUSH_ITEM;
        }
        return EXAM_ITEM;
    }

    public boolean isHeadView(int position) {
        return headViewCount != 0 && position < classDatas.size();
    }

    class ClassNoticeViewHolder extends RecyclerView.ViewHolder{

        private RelativeLayout rlClassItem;
        private TextView tvContent;
        private TextView tvClassSubject;
        private TextView tvClassTime;
        private TextView tvEnter;

        public ClassNoticeViewHolder(@NonNull @NotNull View itemView) {
            super(itemView);
            rlClassItem = itemView.findViewById(R.id.rl_notice_class_item);
            tvContent = itemView.findViewById(R.id.tv_notice_class_title);
            tvClassSubject= itemView.findViewById(R.id.tv_notice_class_subject);
            tvClassTime = itemView.findViewById(R.id.tv_notice_class_time);
            tvEnter = itemView.findViewById(R.id.tv_notice_class_enter);
        }



    }

    class ExamNoticeViewHolder extends RecyclerView.ViewHolder{

        private RelativeLayout rlExamItem;
        private TextView tvTitle;
        private TextView tvSubject;
        private TextView tvTime;
        private TextView tvProgress;
        private TextView tvAnswer;

        public ExamNoticeViewHolder(@NonNull View itemView) {
            super(itemView);

            rlExamItem = itemView.findViewById(R.id.rl_notice_exam_item);
            tvTitle= itemView.findViewById(R.id.tv_notice_exam_title);
            tvSubject = itemView.findViewById(R.id.tv_notice_exam_subject);
            tvTime = itemView.findViewById(R.id.tv_notice_exam_time);
            tvProgress = itemView.findViewById(R.id.tv_notice_exam_progress);
            tvAnswer = itemView.findViewById(R.id.tv_notice_exam_enter);
        }
    }

    class PushNoticeViewHolder extends RecyclerView.ViewHolder{

        private RelativeLayout rlPushItem;
        private TextView tvPushTitle;
        private TextView tvPushSubject;
        private TextView tvPushTime;
        private TextView tvToRead;


        public PushNoticeViewHolder(@NonNull View itemView) {
            super(itemView);

            rlPushItem = itemView.findViewById(R.id.rl_push_item);
            tvPushTitle= itemView.findViewById(R.id.tv_notice_push_name);
            tvPushSubject = itemView.findViewById(R.id.tv_notice_push_subject);
            tvPushTime = itemView.findViewById(R.id.tv_notice_push_time);
            tvToRead = itemView.findViewById(R.id.tv_notice_push_detail);
        }
    }


    private OnNoticeItemClickListener noticeItemClickListener;
    public interface OnNoticeItemClickListener{
        void OnClassItemClickListener(StudentLiveMessageInfo.Data bean, int position);
        void OnExamItemClickListener(StudentExamListInfo.Data.RecordsBean bean,int position);
        void OnPushItemClickListener(ReadPushListInfo.Data.RecordsBean bean);
    }

    public void setNoticeItemClickListener(OnNoticeItemClickListener noticeItemClickListener) {
        this.noticeItemClickListener = noticeItemClickListener;
    }
}
